{*
SPDX-FileCopyrightText: © 2021 Olivier Meunier <olivier@neokraft.net>

SPDX-License-Identifier: AGPL-3.0-only
*}
{{ extends "./base" }}
{{ import "/_libs/forms" }}
{{ import "/_libs/list"}}

{{ block title() }}API Tokens{{ end }}

{{ block mainContent() }}
<h1 class="title text-h2">Your API Tokens</h1>

<div class="prose mb-4">
<p>An API Token is a key that grants you (or another tool like a browser extension)
access to the Readeck API.</p>
<p>Please read the <a href="{{ urlFor(`/docs/api`) }}">API Documentation</a> if you plan to use it.</p>
</div>


<form class="mb-4" action="{{ urlFor() }}" method="post">
  {{ yield csrfField() }}
  <p><button class="btn-primary" type="submit">Create a new API token</button></p>
</form>

{{ if len(.Tokens) > 0 }}
{{ include "/_libs/pagination" .Pagination }}

<turbo-frame id="token-list"
 data-controller="turbo-refresh"
 data-turbo-refresh-interval-value="10"
 data-turbo-refresh-on-value="[data-user-deleted='true']">
  {{ yield list() content }}
  {{ range .Tokens }}
    {{ yield list_item(class="flex items-center") content }}
      <div class="flex-grow">
        <div>
          {{- if .IsEnabled -}}
            {{ yield icon(name="o-check-on", class="svgicon text-green-700") }}
          {{- else -}}
            {{ yield icon(name="o-cross", class="svgicon text-red-700") }}
          {{- end }}
          <a class="link" href="{{ urlFor(`.`, .ID ) }}">{{ .ID }}</a>
          - <small>{{ .Application }}</small>
        </div>
        <div><small>Created on: {{ date(.Created, "2006-01-02") }}
        {{- if .Expires }} (Expires on: {{ date(.Expires, "2006-01-02") }}){{ end -}}
        </small></div>
      </div>
      {{- if .IsDeleted -}}
        <div class="flex items-center max-w-xs"
          data-user-deleted="true">
          <span class="text-red-700 text-xs font-semibold">
            This token will be removed in a few seconds.
          </span>
          <form action="{{ urlFor(`.`, .ID, `delete`) }}" method="post">
            {{ yield csrfField() }}
            <input type="hidden" name="cancel" value="1" />
            <button type="submit"
            class="btn-primary whitespace-nowrap text-sm py-1 ml-2">{{ yield icon(name="o-undo") }} Undo</button>
          </form>
        </div>
      {{- end -}}
    {{ end }}
  {{ end }}
  {{ end }}
</turbo-frame>

{{ include "/_libs/pagination" .Pagination }}
{{ end }}

{{ end }}
